﻿using MongoDB.Driver;
using MongoDB.Driver.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Server.Database
{
    public class MongoDBFacade
    {
        private string _conn;
        private MongoClient _client;
        private MongoServer _server;
        private MongoDatabase _database;

        private MongoClient Client
        {
            get
            {
                
                if (_client == null)
                    _client = new MongoClient(_conn);
                return _client;
            }
        }

        private MongoServer Server
        {
            get
            {
                
                if (_server == null)
                    _server = Client.GetServer();
                
                return _server;
            }
        }

        private MongoDatabase Database
        {
            get
            {

                if (_database == null)
                    _database = Server.GetDatabase("zcdb");
                return _database;
            }
        }
        
        public MongoDBFacade(string conn)
        {
            _conn = conn;
        }

        public bool CollectionExists(string key)
        {
            return Database.CollectionExists(key);
        }

        public MongoCollection<T> GetItems<T>(string key) where T: IEntity
        {
            return Database.GetCollection<T>(key);
        }
    }
}
